
const mongoose = require("mongoose");
const debug = require("debug")("a-sokio-yun:server");
const express = require('express');
const router = express.Router();
// const {dictMod } = require('../db/dictMod.js');
const dictMod = require('../db/dictMod.js');
// const {urlTableMod} = require("../db/model");

router.get('/', async function (req, res) {
    try {
        let userName = req.session.userName;
        if (!userName) {
            return res.json({
                msg: '请刷新浏览器重新登录',
            });
        }

        let query = req.query; //{ page, limit }

        let searchDic = {};

        const count = await dictMod.countDocuments(searchDic);
        const docs = await dictMod
            .find(searchDic)
            .skip((query.page - 1) * query.limit)
            .limit(parseInt(query.limit) || 20)
            .exec();
        res.json({
            code: 0,
            msg: '',
            count: count,
            data: docs,
        });
    } catch (e) {
        console.log(e.message);
        res.json({
            code: -1,
            msg: e.message,
        });
    }
});

router.get('/get-by-name', async function (req, res) {
    try {

        let query = req.query; //{ page, limit }

        let name = query.name;

        const doc = await dictMod.findOne({name: name});
        res.json({
            code: 0,
            msg: '',
            data: doc,
        });
    } catch (e) {
        console.log(e.message);
        res.json({
            code: -1,
            msg: e.message,
        });
    }
});

router.post('/', async function (req, res) {
    try {
        console.log('req.body', req.body);
        let name = req.body.name;
        let value = req.body.value;
        let remark = req.body.remark;

        let doc = await dictMod.create({
            name: name,
            value: value,
            remark: remark,
        });
        res.json({
            code: 0,
            msg: '成功',
            data: doc,
        });
    } catch (e) {
        console.log(e.message);
        res.json({
            code: -1,
            msg: e.message,
        });
    }
});

router.put('/', async function (req, res) {
    try {
        console.log('req.body', req.body);

        let _id = req.body._id;
        let name = req.body.name;
        let value = req.body.value;
        let remark = req.body.remark;



        let doc = await dictMod.findOneAndUpdate({_id: _id},{name:name,value:value,remark:remark},{new: true});
        res.json({
            code: 0,
            msg: '成功',
            data: doc,
        });
    } catch (e) {
        console.log(e.message);
        res.json({
            code: -1,
            msg: e.message,
        });
    }
});

router.delete('/', async function (req, res) {
    try {
        let userName = req.session.userName;
        if (!userName) {
            return res.json({
                msg: '请刷新浏览器重新登录',
            });
        }

        const idArr = req.query.idArr;
        await dictMod.deleteMany({ _id: { $in: JSON.parse(idArr) } });
        res.json({
            code: 0,
            msg: '成功',
        });
    } catch (e) {
        console.log(e.message);
        res.json({
            code: -1,
            msg: e.message,
        });
    }
});








module.exports = router;